/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { DiagnosingMatcher } from '../DiagnosingMatcher.js';
export class StringRegularExpression extends DiagnosingMatcher {
    constructor(pattern) {
        super();
        this.pattern = pattern;
    }
    describeTo(description) {
        description.appendText("a string matching the pattern ").appendValue(this.pattern);
    }
    matchesWithDiagnosingMatcher(actual, mismatchDescription) {
        if (!this.pattern.test(actual.toString())) {
            mismatchDescription.appendText("the string was ").appendValue(actual);
            return false;
        }
        return true;
    }
    /**
     * Creates a matcher that checks if the examined string matches a specified {@link java.util.regex.Pattern}.
     *
     * @param pattern
     *            the pattern to be used.
     * @return The matcher.
     */
    static matchesRegex(pattern) {
        return new StringRegularExpression(pattern);
    }
    /**
     * Creates a matcher that checks if the examined string matches a specified regex.
     *
     * @param regex
     *            The regex to be used for the validation.
     * @return The matcher.
     */
    static matchesStringRegex(regex) {
        return StringRegularExpression.matchesRegex(new RegExp(regex));
    }
}
